package com.market.member.service.login;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.constant.SmsConstant;
import com.block.common.model.dto.LoginReqDTO;
import com.block.common.service.IMemberCommonService;
import com.block.common.util.SmsCodeUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.MemberBaseInfo;
import com.block.db.mapper.MemberBaseInfoMapper;
import com.market.member.enums.UserRegTypeEnum;
import com.market.member.enums.UserTypeEnum;
import com.market.member.model.dto.LoginResultDTO;
import com.block.common.model.dto.NewMemberDTO;
import com.market.member.service.ILoginTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Service
public class CodeLoginServiceImpl extends ILoginTypeService {

    @Autowired
    SmsCodeUtil smsCodeUtil;
    @Resource
    MemberBaseInfoMapper memberBaseInfoMapper;
    @Autowired
    IMemberCommonService memberCommonService;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public ResponseVo toLogin(LoginReqDTO loginReqDTO) {

        String mobile = loginReqDTO.getMobile();
        String verifyCode = loginReqDTO.getLoginCode();
        String otype = SmsConstant.LOGIN_REGISTER;

        MemberBaseInfo memberInfo = memberBaseInfoMapper.getBaseUserInfoByPhoneLock(mobile);
        if (memberInfo != null && "0".equals(String.valueOf(memberInfo.getType()))){
            // 测试账号，校验验证码是否为【888888】
            if (!"888888".equals(verifyCode)){
                return new ResponseVo(AppRspCodeConstant.VERIFY_CODE_ERROR, "该手机号为测试账号，请输入正确的测试验证码");
            }
        }else {
            // 1.验证码校验
            String mobileStr = SecureUtil.md5(mobile).toLowerCase();
            ResponseVo responseVo = smsCodeUtil.checkCode(mobileStr, otype, verifyCode);
            if (!AppRspCodeConstant.SUCCESS.equals(responseVo.getRspCode())){
                return responseVo;
            }
        }

        boolean isNewMember = false;
        if (memberInfo == null){

            NewMemberDTO newMemberDTO = new NewMemberDTO();
            newMemberDTO.setMobile(loginReqDTO.getMobile());
            newMemberDTO.setChannelCode(loginReqDTO.getChannelCode());
            newMemberDTO.setChannelName(loginReqDTO.getChannelName());
            newMemberDTO.setRegAppCode(loginReqDTO.getAppCode());
            newMemberDTO.setRegReqIP(loginReqDTO.getReqIp());
            newMemberDTO.setType(UserTypeEnum.NORMAL.getValue());
            newMemberDTO.setRegType(UserRegTypeEnum.NORMAL.getValue());

            memberInfo = memberCommonService.saveNewMemberInfo(newMemberDTO);
            if (ObjectUtil.isEmpty(memberInfo)){
                return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "注册失败，请重试");
            }
            isNewMember = true;
        }

        LoginResultDTO loginResultDTO = new LoginResultDTO();
        loginResultDTO.setUid(memberInfo.getUid());
        loginResultDTO.setNewMember(isNewMember);
        loginResultDTO.setMemberBaseInfo(memberInfo);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, loginResultDTO);
    }
}
